package cn.iocoder.yudao.module.worker.controller.app.category;

import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.trade.api.config.TradeConfigApi;
import cn.iocoder.yudao.module.trade.api.config.dto.AddressRespDTO;
import cn.iocoder.yudao.module.trade.api.config.dto.TradeConfigRespDTO;
import cn.iocoder.yudao.module.worker.controller.admin.category.vo.CategoryRespVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getClientIP;

import cn.iocoder.yudao.module.worker.controller.app.category.vo.*;
import cn.iocoder.yudao.module.worker.dal.dataobject.category.CategoryDO;
import cn.iocoder.yudao.module.worker.service.category.CategoryService;

@Tag(name = "用户 APP - 商品分类")
@RestController
@RequestMapping("/worker/category")
@Validated
public class AppWorkerCategoryController {

    @Resource
    private CategoryService categoryService;

    @Resource
    private TradeConfigApi tradeConfigApi;

    @PostMapping("/create")
    @Operation(summary = "创建商品分类")
    public CommonResult<Long> createCategory(@Valid @RequestBody AppCategorySaveReqVO createReqVO) {
        return success(categoryService.createCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商品分类")
    public CommonResult<Boolean> updateCategory(@Valid @RequestBody AppCategorySaveReqVO updateReqVO) {
        categoryService.updateCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商品分类")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteCategory(@RequestParam("id") Long id) {
        categoryService.deleteCategory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商品分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppCategoryRespVO> getCategory(@RequestParam("id") Long id) {
        CategoryDO category = categoryService.getCategory(id);
        return success(BeanUtils.toBean(category, AppCategoryRespVO.class));
    }

    @GetMapping("/getCategoryListByParentId")
    @Operation(summary = "获得店员信息")
    @Parameter(name = "parentId", description = "编号", required = true, example = "1024")
    public CommonResult<List<CategoryRespVO>> getCategoryListByParentId(@RequestParam("parentId") Long parentId) {
        TradeConfigRespDTO config = tradeConfigApi.getTradeConfig();

        AppCategoryPageReqVO appCategoryReqVO = new AppCategoryPageReqVO();
        appCategoryReqVO.setParentId(parentId);
        if(!CollectionUtils.isAnyEmpty(config.getOpenCityAreas())){
            AddressRespDTO address = tradeConfigApi.getAddressByIp(getClientIP());
            appCategoryReqVO.setIsOline(config.getOpenCityAreas().contains(address.getProvince()));
        }
        List<CategoryDO> clerkList = categoryService.getAppCategoryList(appCategoryReqVO);
        return success(BeanUtils.toBean(clerkList, CategoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商品分类分页")
    public CommonResult<PageResult<AppCategoryRespVO>> getCategoryPage(@Valid AppCategoryPageReqVO pageReqVO) {
        PageResult<CategoryDO> pageResult = categoryService.getCategoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppCategoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商品分类 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCategoryExcel(@Valid AppCategoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CategoryDO> list = categoryService.getCategoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "商品分类.xls", "数据", AppCategoryRespVO.class,
                        BeanUtils.toBean(list, AppCategoryRespVO.class));
    }

}